﻿//--------------------------------------------------------------------------
// 
//  Copyright (c) Chili Software.  All rights reserved. 
// 
//  File: StoredProcedureQuery.cs
//
//  Description: This class represents a stored procedure query.
// 
//--------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Chili.Opf3.Storages;

namespace Chili.Opf3.Query.StoredProcedures
{
	/// <summary>
	/// This class allows to do a stored procedure call within the Opf3 Framework. It allows
	/// to call a stored procedure by specifying the name of the procedure and parameters.
	/// </summary>
	public class StoredProcedureQuery : IQuery
	{
		private int _timeout = 30;
		private string _sprocName;
		private StoredProcedureParametersCollection _parameters = new StoredProcedureParametersCollection();

		/// <summary>
		/// Creates a new instance of the <see cref="StoredProcedureQuery">StoredProcedureQuery</see> Class.
		/// </summary>
		/// <param name="sprocName">The name of the stored procedure.</param>
		public StoredProcedureQuery(string sprocName)
		{
			if (sprocName == null)
				throw new ArgumentNullException("sprocName");
			_sprocName = sprocName;
		}

		/// <summary>
		/// Creates a new instance of the <see cref="StoredProcedureQuery">StoredProcedureQuery</see> Class.
		/// </summary>
		/// <param name="sprocName">The name of the stored procedure.</param>
		/// <param name="parameters">The parameters for the stored procedure.</param>
		public StoredProcedureQuery(string sprocName, params StoredProcedureNamedParameter[] parameters)
			: this(sprocName)
		{
			_parameters.AddRange(parameters);
		}

		/// <summary>
		/// Gets the name of the stored procedure.
		/// </summary>
		public string StoredProcedureName
		{
			get { return _sprocName; }
		}

		/// <summary>
		/// Gets the parameters associated with the stored procedure.
		/// </summary>
		public StoredProcedureParametersCollection Parameters
		{
			get { return _parameters; }
		}

		#region IQuery Members

		/// <summary>
		/// Converts the <see cref="IQuery">IQuery</see> to an
		/// <see cref="Opf3.Query.CompiledQuery">CompiledQuery</see> that is then executed on a
		/// specific <see cref="Opf3.Storages.IStorage">storage</see>.
		/// </summary>
		/// <returns>
		/// A new <see cref="CompiledQuery">CompiledQuery</see> that has been generated using this instance.
		/// </returns>
		/// <param name="storage">
		/// Class that implements the <see cref="Opf3.Storages.IStorage">IStorage</see>
		/// interface. The query is executed on that storage.
		/// </param>
		/// <exception cref="ArgumentNullException">Storage is null.</exception>
		public CompiledQuery ToCompiledQuery(IStorage storage)
		{
			if (storage == null)
				throw new ArgumentNullException("storage");

			// Use the IStorageCommandBuilder to compile this query.
			IStorageCommandBuilder scb = storage.StorageCommandBuilders.GetStorageCommandBuilder(this);
			return scb.ToCompiledQuery(storage, this);
		}

		/// <summary>
		/// Specifies the wait time before terminating the attempt to execute a command and generating an error.
		/// </summary>
		/// <value>An integer that specifies the wait time in seconds.</value>
		/// <exception cref="ArgumentException">Value is lower then zero.</exception>
		public int Timeout
		{
			get
			{
				return _timeout;
			}
			set
			{
				if (value < 0)
					throw new ArgumentOutOfRangeException("value");

				_timeout = value;
			}
		}

		/// <summary>
		/// Returns the <see cref="Opf3.Query.QueryTypes">QueryType</see> of the SqlQuery.
		/// </summary>
		/// <value>
		/// A member of the <see cref="Opf3.Query.QueryTypes">QueryType</see>
		/// enumeration.
		/// </value>
		public QueryTypes QueryType
		{
			get { return QueryTypes.Unknown; }
		}

		#endregion
	}
}
